package com.letv.android.client.view;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;

import com.letv.android.client.view.LetvPlayGestureLayout.LetvPlayGestureCallBack;

/**
 * 全景手势
 * 
 * @author zhuqiao
 *
 */
public class LetvPanoramaGestureLayout {
    public static final boolean PANORAMA_ENABLE = false;

    /**
     * 手势回调
     */
    protected LetvPlayGestureCallBack mCallbacks;

    private long mDownTime;
    private float mX, mY;

    private float mPreviousX;
    private float mPreviousY;
    private float mLastX;
    private float mLastY;

    /**
     * 无状态
     */
    private static final int NONE = 0;
    /**
     * 拖动状态
     */
    private static final int DRAG = 1;
    /**
     * 缩放状态
     */
    private static final int ZOOM = 2;

    /**
     * 初始缩放比例
     */
    private static final float DEFAULT_SCALE = 1.0f;
    /**
     * 最小缩放比例
     */
    private static final float MIN_SCALE = 0.5f;
    /**
     * 最大缩放比例
     */
    private static final float MAX_SCALE = 2.0f;

    /**
     * 缩放手势
     */
    private ScaleGestureDetector mScaleDetector;
    private float mSaveScale = DEFAULT_SCALE;

    private int mState = NONE;

    public LetvPanoramaGestureLayout(Context context, LetvPlayGestureCallBack callbacks) {
        mCallbacks = callbacks;
        mScaleDetector = new ScaleGestureDetector(context, new OnScaleListener());
    }

    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (mState == ZOOM) {
            return true;
        }

        final int action = event.getAction() & MotionEventCompat.ACTION_MASK;
        final float x = event.getX();
        final float y = event.getY();
        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) {
            mPreviousX = x;
            mPreviousY = y;
            mX = x;
            mY = y;
            mDownTime = System.currentTimeMillis();
        } else if (action == MotionEvent.ACTION_MOVE) {
            return false;
        }
        return false;
    }

    public boolean onTouchEvent(MotionEvent event) {
        mScaleDetector.onTouchEvent(event);
        final int action = event.getAction() & MotionEventCompat.ACTION_MASK;
        final float x = event.getX();
        final float y = event.getY();
        if (action == MotionEvent.ACTION_DOWN) {
            mState = DRAG;
        } else if (action == MotionEvent.ACTION_POINTER_DOWN) {
            mState = ZOOM;
        } else if (action == MotionEvent.ACTION_MOVE) {
            mLastX = x;
            mLastY = y;
            if (mState == DRAG && mCallbacks != null) {
                mCallbacks.setOneFingertouchInfomation(mPreviousX, mPreviousY, mLastX, mLastY);
            }
            mPreviousX = mLastX;
            mPreviousY = mLastY;
        } else if (action == MotionEvent.ACTION_UP) {
            mState = NONE;
            if (System.currentTimeMillis() - mDownTime < 500) {
                // 判定单击事件
                if (Math.abs(mX - event.getX()) < 15 && Math.abs(mY - event.getY()) < 15) {
                    if (mCallbacks != null) {
                        mCallbacks.onSingleTapUp();
                    }
                }
            }
        } else if (action == MotionEvent.ACTION_POINTER_UP) {
            mState = NONE;
        }

        return true;
    }

    /**
     * 缩放手势监听
     * 
     * @author user
     *
     */
    private class OnScaleListener extends SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float scaleFactor = detector.getScaleFactor();
            scaleFactor = Math.min(1.05f, Math.max(0.95f, scaleFactor));
            final float originalScale = mSaveScale;
            mSaveScale *= scaleFactor;
            if (mSaveScale > MAX_SCALE) {
                mSaveScale = MAX_SCALE;
                scaleFactor = MAX_SCALE / originalScale;
            } else if (mSaveScale < MIN_SCALE) {
                mSaveScale = MIN_SCALE;
                scaleFactor = MIN_SCALE / originalScale;
            }

            if (mCallbacks != null) {
                mCallbacks.setTwoScale(mSaveScale);
            }
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            mState = ZOOM;
            return true;
        }

    }

}
